package ldh.im.fxgui.pane;

import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import ldh.im.ImConfig;
import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.dao.ImMessageDao;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxbase.util.*;
import ldh.im.fxgui.util.ImageUtil;
import ldh.rpc.cache.CacheService;

/**
 * 文件面板
 */
public class FileMessagePane extends BaseMessagePane {

    private ImMessageDao imMessageDao = new ImMessageDao();

    @Override
    public Node createPane(ImMessage message, boolean isReceive, boolean isRef) {
        if (message.getMessageType() != MessageType.File) {
            throw new RuntimeException("不支持这种消息类型" + message.getMessageType());
        }
        CacheService cacheService = RpcUtil.getRpcBootstrap().getRpcConfig().getCacheService();
        String userId = (String) cacheService.getCache(ImConfig.LOGIN_USER_ID);

        StackPane stackPane = new StackPane();
        stackPane.setAlignment(Pos.CENTER_LEFT);
        stackPane.setStyle("-fx-padding: 10px");
        stackPane.getStyleClass().add("message-item-file");

//        HBox hBox = new HBox();
//        Label fileLabel = new Label(message.getMessageExt().getOriginalName());
//        FontIcon fontIcon = FontIcon.of(FontAwesome.findByDescription("fa-file"), 20, Color.GREEN);
//        fileLabel.setGraphic(fontIcon);
//
//        Region region = new Region();
//        hBox.getChildren().addAll(fileLabel, region);
//        HBox.setHgrow(region, Priority.ALWAYS);
//        hBox.setAlignment(Pos.CENTER_LEFT);
//
//        if (!isRef && message.getFileStatus() != ImMessageFileStatus.downloaded && isReceive) {
//            JFXButton jfxButton = new JFXButton();
//            FontIcon downIcon = FontIcon.of(FontAwesome.findByDescription("fa-arrow-down"), 20, Color.GREEN);
//            jfxButton.setGraphic(downIcon);
//
//            hBox.getChildren().addAll(jfxButton);
//
//            jfxButton.setOnAction(e->{ ;
//                downloadFile(userId, message);
//            });
//        }

        stackPane.getChildren().add(buildFilePane(message));

        if (!isRef) {
            buildPopup(stackPane, message);
        }
        return stackPane;
    }

    private GridPane buildFilePane(ImMessage message) {
        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        Label fileNameLabel = new Label(message.getMessageExt().getOriginalName());
        fileNameLabel.getStyleClass().add("primary-text");
        Label fileSizeLabel = new Label(FileUtil.formatFileSize(message.getMessageExt().getSize()));
        fileSizeLabel.getStyleClass().add("tip-text");

        String imageFilePath = ImageUtil.getFileImagePath(FileUtil.getSuffix(message.getMessageExt().getOriginalName()));
        Image image = new Image(imageFilePath);
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(40);
        imageView.setFitHeight(50);
        gridPane.getChildren().addAll(fileNameLabel, fileSizeLabel, imageView);
        GridPane.setConstraints(fileNameLabel, 0, 0);
        GridPane.setConstraints(fileSizeLabel, 0, 1);
        GridPane.setConstraints(imageView, 1, 0, 1, 2);
        RowConstraints row1 = new RowConstraints();
        row1.setVgrow(Priority.ALWAYS);
        RowConstraints row2 = new RowConstraints();
        gridPane.getRowConstraints().addAll(row1, row2);
        return gridPane;
    }


}
